package com.quectel.cms.controller.datav;

import cn.hutool.core.bean.BeanUtil;
import com.quectel.business.common.util.sys.SysCodeUtils;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.sys.SysCodeKeyConstants;
import com.quectel.core.module.car.dto.datav.CarCountDto;
import com.quectel.core.module.car.service.CarService;
import com.quectel.core.module.device.dto.datav.DeviceTypeCountDto;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.household.dto.datav.FocusGroupsCountDto;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.core.module.village.dto.VillageRoomDto;
import com.quectel.core.module.village.dto.datav.BuildingRoomHouseholdCount;
import com.quectel.core.module.village.dto.datav.RoomCountDto;
import com.quectel.core.module.village.service.VillageRoomService;
import com.quectel.sys.module.sys.dto.SysCodeDto;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:10:19
 */
@RestController
@RequestMapping("datav/overview")
@Tag(name = "DataVOverviewController", description = "总览相关接口")
public class DataVOverviewController extends BaseController {

    @DubboReference
    private DeviceService deviceService;
    @DubboReference
    private VillageRoomService villageRoomService;
    @DubboReference
    private HouseholdService householdService;
    @DubboReference
    private CarService carService;


    @GetMapping("queryHasFocusHouseholdRooms")
    @Operation(summary = "查询具有重点人员的房间")
    public Response<List<VillageRoomDto>> queryHasFocusHouseholdRooms(
            @Parameter(description = "楼栋id") Long buildingId
    ) {
        return Response.<List<VillageRoomDto>>ok().wrap(householdService.queryHasFocusHouseholdRooms(buildingId));
    }


    @GetMapping("countRoom")
    @Operation(summary = "房屋信息统计")
    public Response<RoomCountDto> countHousehold(
            @Parameter(description = "小区id") @RequestParam(required = false) Long villageId
    ) {
        Map<String, Object> params = Request.parseParams(httpServletRequest);
        super.fillAuthorityData(params);
        return Response.<RoomCountDto>ok().wrap(villageRoomService.countRoom(params));
    }

    @GetMapping("focusGroupsCount")
    @Operation(summary = "重点人员统计")
    public Response<List<FocusGroupsCountDto>> focusGroupsCount(
            @Parameter(description = "小区id") @RequestParam(required = false) Long villageId
    ) {
        Map<String, Object> params = Request.parseParams(httpServletRequest);
        super.fillAuthorityData(params);

        List<SysCodeDto> sysCodeDtos = SysCodeUtils.easyToGetByMark(SysCodeKeyConstants.HOUSEHOLD_TAG);
        //sysCodeDto在core-service不存在所以转为map(这里不转map dubbo也会自动转)
        params.put("tags", sysCodeDtos.stream().map(v -> BeanUtil.beanToMap(v)).collect(Collectors.toList()));
        Map<String, Double> result = householdService.focusGroupsCount(params);

        List<FocusGroupsCountDto> list = new ArrayList<>();
        for (Map.Entry<String, Double> entry : result.entrySet()) {
            FocusGroupsCountDto focusGroupsCountDto = new FocusGroupsCountDto();
            focusGroupsCountDto.setName(SysCodeUtils.easyToGetName(SysCodeKeyConstants.HOUSEHOLD_TAG, entry.getKey()));
            focusGroupsCountDto.setCount(entry.getValue().intValue());
            list.add(focusGroupsCountDto);
        }

        return Response.<List<FocusGroupsCountDto>>ok().wrap(list);
    }

    @GetMapping("buildingCount")
    @Operation(summary = "楼栋信息相关统计")
    public Response<BuildingRoomHouseholdCount> focusGroupsCount(
            @Parameter(description = "小区id") @RequestParam(required = false) Long villageId,
            @Parameter(description = "楼栋id") @RequestParam(required = false) Long buildingId
    ) {
        Map<String, Object> params = Request.parseParams(httpServletRequest);
        super.fillAuthorityData(params);

        BuildingRoomHouseholdCount buildingRoomHouseholdCount = new BuildingRoomHouseholdCount();
        buildingRoomHouseholdCount.setHouseholdCountDto(householdService.countHousehold(params));
        buildingRoomHouseholdCount.setRoomCountDto(villageRoomService.countRoom(params));

        return Response.<BuildingRoomHouseholdCount>ok().wrap(buildingRoomHouseholdCount);
    }

    @GetMapping("countCar")
    @Operation(summary = "车辆信息统计")
    public Response<CarCountDto> countCar(
            @Parameter(description = "小区id") @RequestParam(required = false) Long villageId
    ) {
        Map<String, Object> params = Request.parseParams(httpServletRequest);
        super.fillAuthorityData(params);
        return Response.<CarCountDto>ok().wrap(carService.countCar(params));
    }

    @GetMapping("countDeviceCategory")
    @Operation(summary = "按照设备类型统计设备")
    public Response<List<DeviceTypeCountDto>> countDeviceCategory(
            @Parameter(description = "小区id") @RequestParam(required = false) Long villageId
    ) {
        Map<String, Object> params = Request.parseParams(httpServletRequest);
        super.fillAuthorityData(params);
        return Response.<List<DeviceTypeCountDto>>ok().wrap(deviceService.countDeviceType(params));
    }
}
